<template>
 <el-dialog
  class="commonDialog"
  :title="parentTitle === 1 ? '新增设备类型' : '编辑设备类型'"
  :visible.sync="visible"
  width="500px"
  :before-close="cancelModule">
        <el-form ref="addMeal" :rules="rules" class="commonForm mt45" :model="addMeal" label-width="40%">
            <el-form-item label="设备名称:" prop="equimentTypeName" required>
                 <el-input class="commonInput" v-model="addMeal.equimentTypeName"></el-input>
            </el-form-item>
            <el-form-item label="备注:" prop="remark" >
                 <el-input type="textarea" class="commonTextarea" v-model="addMeal.remark"></el-input>
            </el-form-item>
            <el-form-item class="btnsLine mt60">
                <el-button class="commonButton" @click="cancelModule(false)">取消</el-button>
                <el-button class="commonButton" @click.native="submitForm('addMeal')" v-if="parentTitle === 1">确定</el-button>
                <el-button class="commonButton" @click.native="submitEditForm('addMeal')" v-if="parentTitle === 2">确定</el-button>
            </el-form-item>
        </el-form>
</el-dialog>
</template>
<script>
import moment from 'moment';
import axios from 'axios';
import { formatDate } from '@/filters/index';
export default {
    props: ['parentDialogVisible', 'getParentTableData', 'parentTitle', 'parentCurrentMeal'],
    data() {
        return {
            urlPrev: 'http://39.106.7.166:8097/',
            visible: false,
            addMeal: {
                restaurantId: '1',
                equimentTypeName: '',
                remark:'',
                userName: '张三',
                mobile: '13232326253'
            },
            rules: {
                equimentTypeName: [
                    { required: true, message: '请输入设备名称', trigger: 'blur' }
                ]
            },
        }
    },
    mounted(){
         this.visible = true;
         if(this.parentTitle === 2) {
             this.initFormData();
         }
    },
    methods: {
        // 编辑初始化表单
        initFormData(){
            this.addMeal = this.parentCurrentMeal;
            this.addMeal.equimentTypeName =  this.parentCurrentMeal.equipmentName;
        },
         //关闭弹框
        cancelModule(val) {
            this.$emit('cancelModule', val);
        },
        // 新增餐别
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
            if (valid) {
                this.mealSave();
            } else {
                console.log('提交错误!');
                return false;
            }
            });
        },
        // 编辑餐别
         submitEditForm(formName) {
            this.$refs[formName].validate((valid) => {
            if (valid) {
                this.mealEdit();
            } else {
                console.log('提交错误!');
                return false;
            }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        handleClick(tab, event) {
            console.log(tab, event);
        },
        // 添加
        mealSave() {
            const url = this.urlPrev + `api/bdequitype/addequitype`;
            axios({ method: 'post', url: url, data: this.addMeal })
                .then(rsp => {
                    if (rsp.data.status == 1) {
                        this.$emit('getParentTableData');
                         this.$message({
                            message: '保存成功',
                            type: 'success',
                        });
                    } else {
                        this.$message({
                            message: rsp.data.message,
                            type: 'error',
                        });
                    }
                })
                .catch(err => console.log(err));
                return false;
        },
         // 编辑
        mealEdit() {
            const url = this.urlPrev + `api/bdequitype/editequitype`;
            axios({ method: 'post', url: url, data: this.addMeal })
                .then(rsp => {
                    if (rsp.data.status == 1) {
                        this.$emit('getParentTableData');
                         this.$message({
                            message: '编辑成功',
                            type: 'success',
                        });
                    } else {
                        this.$message({
                            message: rsp.data.message,
                            type: 'error',
                        });
                    }
                })
                .catch(err => console.log(err));
                return false;
        }
    }
}
</script>
<style lang="scss" scoped>

</style>